SUBROUTINE UPDDIAG( &
 & KIDIA,KFDIA,KLON,KLEV,KLEVS,KLEVSN,KTILES,KVTYPES, &
 & KDHVTLS,KDHFTLS,KDHVTSS,KDHFTSS, &
 & KDHVTTS,KDHFTTS,KDHVSSS,KDHFSSS, &
 & KDHVIIS,KDHFIIS,KDHVWLS,KDHFWLS, &
 & KDHVRESS,KDHFRESS, &
 & KDHVCO2S,KDHFCO2S,KDHVBIOS,KDHFBIOS, &
 & KDHVVEGS,KDHFVEGS,KDHVBVOCS, &
 & PTSPHY,KIDIAF,KFDIAF, &
 & PEVAPTIU,&  
 & PAN,PAG,PRD,PRSOIL_STR,PRECO,PCO2FLUX,PCH4FLUX,PBVOCFLUX, &
 & PLAI,PBIOM,PBLOSS,PBGAIN,PBIOMSTR,PBIOMSTR2, &
 & PTAIR,PQAIR,PUWIND,PVWIND,PPSURF,&
 & PFRSO,PSRSOD,PFRTH,PSRTHD,PALB,PFTLHEV,PFTLHSB, &
 & PDIFTQ,PDIFTS,PSTRTU,PSTRTV,PWSAE1,PSNSE1,PWLE1, &
 & PCRF    ,PCSF    ,PLSRF,  PLSSF,PFWRO1, PFWROD, PFWMLT,&
 & PDHTLS,PDHTSS,PDHTTS,PDHSSS,PDHIIS,PDHWLS,PDHRESS,&
 & PT2M,PD2M,PDHCO2S,PDHBIOS,PDHVEGS,PDHBVOCS)

! (C) Copyright 2010- ECMWF.
!
! This software is licensed under the terms of the Apache Licence Version 2.0
! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
! In applying this licence, ECMWF does not waive the privileges and immunities
! granted to it by virtue of its status as an intergovernmental organisation
! nor does it submit to any jurisdiction.
!
!**** UPDDIAG - INTERFACE ROUTINE TO COPY IFS VARIABLES TO
!		OFFLINE CODE DIAGNOSTICS BUFFERS

!     S. Boussetta/G.Balsamo May 2010 Add CTESSEL based on:
!        Marita Voogt (KNMI) "C-Tessel" 09/2005
!        Sebastien Lafont (ECMWF) "C-TESSEL"

!     E. Dutra May 2014, add KIDIAF,KFDIAF for NPROMA implementation 

!
! INTERFACE
!
!    *KLON*         NUMBER OF GRID POINTS PER PACKET
!    *KIDIA*        FIRST POINT
!    *KFDIA*        LAST POINT
!    *KLEV*         NUMBER OF LOWEST LEVEL
!    *KLEVS*        SOIL LAYERS
!    *KLEVSN*       SNOW LAYERS
!    *KTILES*       NUMBER OF TILES (I.E. SUBGRID AREAS WITH DIFFERENT 
!                   OF SURFACE BOUNDARY CONDITION)

!    *KVTYPES*      NUMBER OF VEGETATION TYPES

!    *KDHVTLS*      Number of variables for individual tiles
!    *KDHFTLS*      Number of fluxes for individual tiles
!    *KDHVTSS*      Number of variables for snow energy budget
!    *KDHFTSS*      Number of fluxes for snow energy budget
!    *KDHVTTS*      Number of variables for soil energy budget
!    *KDHFTTS*      Number of fluxes for soil energy budget
!    *KDHVIIS*      Number of variables for interception water budget
!    *KDHFIIS*      Number of fluxes for interception water budget
!    *KDHVSSS*      Number of variables for snow water budget
!    *KDHFSSS*      Number of fluxes for snow water budget
!    *KDHVWLS*      Number of variables for soil water budget
!    *KDHFWLS*      Number of fluxes for soil water budget
!    *KDHVRESS*     Number of variables for resistances 
!    *KDHFRESS*     Number of fluxes for resistances

!    *KDHVCO2S*     Number of variables for CO2
!    *KDHFCO2S*     Number of fluxes for CO2
!    *KDHVBIOS*     Number of variables for biomass
!    *KDHFBIOS*     Number of fluxes for biomass
!    *KDHVVEGS*     Number of variables for vegetation
!    *KDHFVEGS*     Number of fluxes for vegetation


!
!    *PTSPHY*       time step length
!    *PDHTLS*	    surface fluxes
!    *PDHTSS*	    snow fluxes
!    *PDHTTS*	    soil T fluxes
!    *PDHSSS*	    snow mass fluxes
!    *PDHIIS*	    interception mass fluxes
!    *PDHWLS*	    soil water fluxes
!    *PDHRESS*      resistances 
!
!    *PT2M*         Screen-level air temperature  
!    *PD2M*         Screen-level air dew point temperature 
!
!     PWSAE1	 : tendency of soil water content
!     PWLE1	 : tendency of interception reservoir content
!     PSNSE1	 : tendency of snow mass

!    *PDHCO2S*      CO2 fluxes per vegetation type
!    *PDHBIOS*      biomass per vegetation type
!    *PDHVEGS*      vegetation variables per vegetation type



!     PSRSOD     : SURFACE SHORTWAVE RADIATIVE FLUX DOWNWARDS.
!     PSRTHD     : SURFACE LONGWAVE RADIATIVE FLUX DOWNWARDS.
!     PFRSO      : SHORTWAVE RADIATIVE FLUX.
!     PFRTH      : LONGWAVE RADIATIVE FLUX.
!     PALB       : MODEL SURFACE SHORTWAVE ALBEDO.
!     PFTLHEV    : SURFACE LATENT HEAT FLUX (SNOW FREE FRACTION)
!     PFTLHSB    : SURFACE LATENT HEAT FLUX (SNOW COVERED FRACTION)
!     PDIFTQ     : TURBULENT FLUX OF MOISTURE
!     PDIFTS     : TURBULENT FLUX OF ENTHALPY (OR DRY STATIC ENERGY).
!     PSTRTU     : TURBULENT MOMENTUM FLUX 
!     PSTRTV     : TURBULENT MOMENTUM FLUX 
!     PLSRF      : LARGE SCALE RAINFALL
!     PCRF       : CONVECTIVE RAINFALL
!     PLSSF      : LARGE SCALE SNOWFALL
!     PCSF       : CONVECTIVE SNOWFALL
!     PFWRO1     : SURFACE RUNOFF
!     PFWROD     : DEEP RUNOFF
!     PFWMLT     : SNOW MELT

!     PAN        : NET CO2 ASSIMILATION OVER CANOPY         
!     PAG        : GROSS CO2 ASSIMILATION OVER CANOPY   
!     PRD        : DARK RESPIRATION                         
!     PRSOIL_STR : RESPIRATION FROM SOIL AND STRUCTURAL BIOMASS
!     PRECO      : ECOSYSTEM RESPIRATION 
!     PCO2FLUX   : CO2 FLUX
!     PCH4FLUX   : CH4 FLUX
!     PBVOCFLUX  : BVOC FLUX
!     PLAI       : LAI (M2/M2)
!     PBIOM      : BIOMASS (KG/M2)
!     PBLOSS     : BIOMASS LOSS (KG/M2)
!     PBGAIN     : BIOMASS GAIN (KG/M2)
!     PBIOMSTR   : ABOVE GROUND STRUCTURAL BIOMASS (KG/M2)
!     PBIOMSTR2  : BELOW GROUND STRUCTURAL BIOMASS (KG/M2)
!     PWSAE1	 : tendency of soil water content
!     PWLE1	 : tendency of interception reservoir content
!     PSNSE1	 : tendency of snow mass
!     PTAIR      : AIR TEMPERATURE (forcing T+1, end of timestep)
!     PQAIR      : AIR SPECIFIC HUMIDITY (forcing T+1, end of timestep)
!     PUWIND     : U WIND SPEED (forcing T+1, end of timestep)
!     PVWIND     : V WIND SPEED (forcing T+1, end of timestep)



!
USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOETHF   , ONLY : RHOH2O
USE YOESOIL1S, ONLY : RDAW
USE YOMGPD1S , ONLY : VDIEWSS  ,VDINSSS  ,VDISSHF 
USE YOMGDI1S , ONLY : D1STISRD ,D1STISRU ,D1STITRD ,D1STITRU ,&
            &D1STIH   ,D1STILE  ,D1STIGFL ,D1STII   ,D1STIEVAP,&
            &D1SNFR   ,D1SNTFR  ,D1SNAFR  ,&
            &D1SRFLD  ,D1SRFLU  ,D1TRFLD  ,D1TRFLU  ,&
            &D1AHFS   ,D1AHFL   ,D1MOMU   ,D1MOMV   ,&
            &D1STNSRD ,D1STNSRU ,D1STNTRD ,D1STNTRU ,&
            &D1STNGFL ,D1STNDH  ,D1STNM   ,&
            &D1ST1SRD ,D1ST1SRU ,D1ST1TRD ,D1ST1TRU ,&
            &D1ST1H   ,D1ST1LE  ,D1STAGFL ,D1STASF  ,&
            &D1SSFL   ,D1SSFC   ,D1SWNJQ  ,D1SWNM   ,&
            &D1SWLIT  ,D1SWLJQ  ,&
            &D1SW1JBG ,D1SW1TF  ,D1SWAGFL ,D1SW1RI  ,&
            &D1SWARS  ,D1SWAEXT ,D1SW1M   ,D1SWAC   ,&
            &D1SW1C   ,D1STIFR  ,D1STITK  ,D1STIALB ,&
            &D1SVTAN  ,D1SVTAG  ,D1SVTRD  ,D1SVTRSOIL_STR,D1SVTRECO,&
	    &D1SVTCO2FLUX,D1SVTRNOQ10,&
	    &D1SAN    ,D1SAG    ,D1SRD    ,D1SRSOIL_STR ,D1SRECO,&
	    &D1SCO2FLUX,D1SCH4FLUX, D1SBVOCFLUX, &
	    &D1SLAI   ,D1SBIOM  ,D1SBLOSS ,D1SBGAIN ,D1SBIOMSTR,&
            &D1SBIOMSTRB,&
	    &D1SVTLAI ,D1SVTBIOM,D1SVTBLOSS,D1SVTBGAIN,&
	    &D1SVTBIOMSTR,D1SVTBIOMSTRB,&
	    &D1SVTGC  ,D1SVTGA  ,D1SVTF2  ,D1SVTDS,D1SVTDMAX,&
	    &D1SVTLE  ,D1SVTFR  ,D1SWFR    ,&
	    &D1STAIR  ,D1SQAIR  ,D1SUWIND  ,D1SVWIND,&
            &D1SNDEPTH,D1SWAFR  ,D1SRADTK ,&
            &D1SAVTK  ,D1SVEGTK ,&
            &D1SGHF   ,D1SALB   ,&
            &D1SVEGEV ,D1SDSH   ,&
            &D1SWDS   ,&
            &D1SSDS   ,D1SWLDS  ,&
            &D1SLSRF  ,D1SCRF   ,D1SLSSF  ,D1SCSF   ,&
            &D1STE    ,D1STRO   ,D1STSRO  ,D1SMLT  ,&
	        &D1SSDSSL,&
	        &D1SVTRC  ,D1SVTRA ,&
            &D1T2M    ,D1D2M,&
            &D1WFLUXRF,D1HFLUXRF,D1WSN,&
            &D1STIEVAPU,D1STISKC,D1SPSURF, &
            &D1SBVOCST1,D1SBVOCST2

            
IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA
INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEVS
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEVSN
INTEGER(KIND=JPIM),INTENT(IN)    :: KTILES 

INTEGER(KIND=JPIM),INTENT(IN)    :: KVTYPES 

INTEGER(KIND=JPIM),INTENT(IN)    :: KDHVTLS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHFTLS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHVTSS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHFTSS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHVTTS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHFTTS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHVSSS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHFSSS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHVIIS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHFIIS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHVWLS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHFWLS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHVRESS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHFRESS

INTEGER(KIND=JPIM),INTENT(IN)    :: KDHFCO2S 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHVCO2S
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHFBIOS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHVBIOS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHFVEGS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHVVEGS
INTEGER(KIND=JPIM),INTENT(IN)    :: KDHVBVOCS

REAL(KIND=JPRB),INTENT(IN)       :: PTSPHY
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIAF
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIAF

REAL(KIND=JPRB)   ,INTENT(IN)    :: PSRSOD(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSRTHD(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRSO(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFRTH(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PALB(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFTLHEV(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFTLHSB(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTQ(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTS(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRTU(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSTRTV(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWSAE1(KLON,KLEVS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSNSE1(KLON,KLEVSN)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWLE1(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLSRF(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCRF(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCSF(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLSSF(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFWRO1(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFWROD(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFWMLT(KLON)

REAL(KIND=JPRB)   ,INTENT(IN)    :: PAN(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAG(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRD(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRSOIL_STR(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRECO(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCO2FLUX(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCH4FLUX(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBVOCFLUX(KLON,KDHVBVOCS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLAI(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBIOM(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBLOSS(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBGAIN(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBIOMSTR(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBIOMSTR2(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTAIR(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQAIR(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUWIND(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVWIND(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPSURF(KLON)

REAL(KIND=JPRB)   ,INTENT(IN)    :: PDHTLS(KLON,KTILES,KDHVTLS+KDHFTLS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDHTSS(KLON,KLEVSN,KDHVTSS+KDHFTSS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDHTTS(KLON,KLEVS,KDHVTTS+KDHFTTS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDHSSS(KLON,KLEVSN,KDHVSSS+KDHFSSS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDHIIS(KLON,KDHVIIS+KDHFIIS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDHWLS(KLON,KLEVS,KDHVWLS+KDHFWLS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDHRESS(KLON,2,KDHVRESS+KDHFRESS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT2M(KLON)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PD2M(KLON)

REAL(KIND=JPRB)   ,INTENT(IN)    :: PDHCO2S(KLON,KVTYPES,KDHVCO2S+KDHFCO2S)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDHBIOS(KLON,KVTYPES,KDHVBIOS+KDHFBIOS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDHVEGS(KLON,KVTYPES,KDHVVEGS+KDHFVEGS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDHBVOCS(KLON,KVTYPES,KDHVBVOCS)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEVAPTIU(KLON,KTILES)

INTEGER(KIND=JPIM) :: JTILE,JL,jk,JLK
REAL(KIND=JPRB) :: ZEPS
REAL(KIND=JPRB) :: ZEPSILON

! balans sluitingen
INTEGER(KIND=JPIM)   :: I1
REAL(KIND=JPRB)      :: HELP1
real(kind=jprb) :: help2(13),help3(13)

!* ==============================================================
!* ==============================================================
!*           IMPORTANT 
!* Use KIDIA,KFDIA for input array P* that are defined over KLON points 
!* Use KIDIAF:KFDIAF  for module D* arrays that are defined over all NPOINTS 
!* Be carafull when using the D* in calculations for other D* arrays 
!* ==============================================================
!* ==============================================================

ZEPS=1.E-5
ZEPSILON=EPSILON(ZEPSILON)
!* calculations on JL 
DO JL=KIDIA,KFDIA
  JLK = JL-KIDIA+KIDIAF ! index on the full array 
  D1SWDS(JLK)=PTSPHY*RHOH2O*DOT_PRODUCT(PWSAE1(JL,:),RDAW(:))
  DO JK=1,KLEVS
    D1SSDSSL(JLK,JK)=PTSPHY*RHOH2O*PWSAE1(JL,JK)*RDAW(JK)
  ENDDO
ENDDO

!*  -- no calculations just memory swap 

VDISSHF(KIDIAF:KFDIAF) = PDIFTS(KIDIA:KFDIA,KLEV)
VDIEWSS(KIDIAF:KFDIAF) = PSTRTU(KIDIA:KFDIA,KLEV)
VDINSSS(KIDIAF:KFDIAF) = PSTRTV(KIDIA:KFDIA,KLEV)


! Tile energy fluxes
D1STISRD(KIDIAF:KFDIAF,:) = PDHTLS(KIDIA:KFDIA,:,4)
D1STISRU(KIDIAF:KFDIAF,:) = PDHTLS(KIDIA:KFDIA,:,5)
D1STITRD(KIDIAF:KFDIAF,:) = PDHTLS(KIDIA:KFDIA,:,6)
D1STITRU(KIDIAF:KFDIAF,:) = PDHTLS(KIDIA:KFDIA,:,7)
D1STIH(KIDIAF:KFDIAF,:)   = PDHTLS(KIDIA:KFDIA,:,8)
D1STILE(KIDIAF:KFDIA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S(KIDIA:KFDIA,1,16) Not available in surf 
! Soil energy fluxes
D1ST1SRD(KIDIAF:KFDIAF) = PDHTTS(KIDIA:KFDIA,1,5)
D1ST1SRU(KIDIAF:KFDIAF) = PDHTTS(KIDIA:KFDIA,1,6)
D1ST1TRD(KIDIAF:KFDIAF) = PDHTTS(KIDIA:KFDIA,1,7)
D1ST1TRU(KIDIAF:KFDIAF) = PDHTTS(KIDIA:KFDIA,1,8)
D1ST1H(KIDIAF:KFDIAF)   = PDHTTS(KIDIA:KFDIA,1,9)
D1ST1LE(KIDIAF:KFDIAF)  = PDHTTS(KIDIA:KFDIA,1,10)
D1STAGFL(KIDIAF:KFDIAF,:) = PDHTTS(KIDIA:KFDIA,:,11)
D1STASF(KIDIAF:KFDIAF,:)  = PDHTTS(KIDIA:KFDIA,:,12)
! Snow water fluxes
D1SSFL(KIDIAF:KFDIAF)  = PDHSSS(KIDIA:KFDIA,1,2)
D1SSFC(KIDIAF:KFDIAF)  = PDHSSS(KIDIA:KFDIA,1,3)
D1SWNJQ(KIDIAF:KFDIAF) = PDHSSS(KIDIA:KFDIA,1,4)
D1SWNM(KIDIAF:KFDIAF)  = PDHSSS(KIDIA:KFDIA,1,5)
D1WFLUXRF(KIDIAF:KFDIAF) = 0._JPRB ! PDHSSS(KIDIA:KFDIA,1,6) Not available in surf 
D1WSN(KIDIAF:KFDIAF) = 0._JPRB ! PDHSSS(KIDIA:KFDIA,1,7) Not available in surf 
! Interception water fluxes
D1SWLIT(KIDIAF:KFDIAF) = PDHIIS(KIDIA:KFDIA,2)+PDHIIS(KIDIA:KFDIA,3)
D1SWLJQ(KIDIAF:KFDIAF) = PDHIIS(KIDIA:KFDIA,4)
!write(6,*) 'upddiag chech interceptie balans'
!write(6,*) 'interceptie',D1SWLIT
!write(6,*) 'evaporatie',D1SWLjq
! Soil water fluxes
D1SW1JBG(KIDIAF:KFDIAF) = PDHWLS(KIDIA:KFDIA,1,9)	! bare ground evaporation 
					        ! with mismatches from 
                                                ! intercep & snow tiles
D1SW1TF(KIDIAF:KFDIAF)  = PDHWLS(KIDIA:KFDIA,1,3)+PDHWLS(KIDIA:KFDIA,1,4)
D1SWAGFL(KIDIAF:KFDIAF,:) = PDHWLS(KIDIA:KFDIA,:,8)

D1SW1RI(KIDIAF:KFDIAF) = PDHWLS(KIDIA:KFDIA,1,6)	! surface runoff
D1SWARS(KIDIAF:KFDIAF,:) = 0.			! excess runoff
D1SWAEXT(KIDIAF:KFDIAF,:) = PDHWLS(KIDIA:KFDIA,:,7)
D1SW1M(KIDIAF:KFDIAF) = PDHSSS(KIDIA:KFDIA,1,5)
D1SW1C(KIDIAF:KFDIAF) = PDHWLS(KIDIA:KFDIA,1,10)
D1SWAC(KIDIAF:KFDIAF,:) = 0.			! clipping

! Resistances for low and high vegetation type
! D1SVTRC(KIDIAF:KFDIAF,:) = PDHRESS(KIDIA:KFDIA,:,1)  PDHRESS  not used in surf...
! D1SVTRA(KIDIAF:KFDIAF,:) = PDHRESS(KIDIA:KFDIA,:,2)  PDHRESS  not used in surf...


! CO2 fluxes per vegetation type (normalized to grid)
D1SVTAG(KIDIAF:KFDIAF,:) =        PDHCO2S(KIDIA:KFDIA,:,1)
D1SVTRD(KIDIAF:KFDIAF,:) =        PDHCO2S(KIDIA:KFDIA,:,2)
D1SVTAN(KIDIAF:KFDIAF,:) =        PDHCO2S(KIDIA:KFDIA,:,3)
D1SVTRSOIL_STR(KIDIAF:KFDIAF,:) = PDHCO2S(KIDIA:KFDIA,:,4)
D1SVTRECO(KIDIAF:KFDIAF,:) =      PDHCO2S(KIDIA:KFDIA,:,5)
D1SVTCO2FLUX(KIDIAF:KFDIAF,:) =   PDHCO2S(KIDIA:KFDIA,:,6)
D1SVTRNOQ10(KIDIAF:KFDIAF,:) =    PDHCO2S(KIDIA:KFDIA,:,7)
! CO2 flux per grid
D1SAN(KIDIAF:KFDIAF)=PAN(KIDIA:KFDIA)
D1SAG(KIDIAF:KFDIAF)=PAG(KIDIA:KFDIA)
D1S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F:KFDIAF)+PDHTLS(KIDIA:KFDIA,JTILE,1)*PDHTLS(KIDIA:KFDIA,JTILE,2)
ENDDO
D1SVEGTK(KIDIAF:KFDIAF) = (MAX(ZEPS,PDHTLS(KIDIA:KFDIA,4,1))*PDHTLS(KIDIA:KFDIA,4, 2)+ &
     &      PDHTLS(KIDIA:KFDIA,6,1)*PDHTLS(KIDIA:KFDIA,6,2))/  &
     &     (MAX(ZEPS,PDHTLS(KIDIA:KFDIA,4,1))+PDHTLS(KIDIA:KFDIA,6,1))
D1SGHF(KIDIAF:KFDIAF) = PDHTTS(KIDIA:KFDIA,1,14)
D1SRADTK(KIDIAF:KFDIAF) = 0.	! not calculated in vdfmain
D1SRADTK(KIDIAF:KFDIAF) =(PDHTLS(KIDIA:KFDIA,1,1)*(PDHTLS(KIDIA:KFDIA,1, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,2,1)*(PDHTLS(KIDIA:KFDIA,2, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,3,1)*(PDHTLS(KIDIA:KFDIA,3, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,4,1)*(PDHTLS(KIDIA:KFDIA,4, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,5,1)*(PDHTLS(KIDIA:KFDIA,5, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,6,1)*(PDHTLS(KIDIA:KFDIA,6, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,7,1)*(PDHTLS(KIDIA:KFDIA,7, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,8,1)*(PDHTLS(KIDIA:KFDIA,8, 2))**4)**(0.25_JPRB) 
IF (KTILES > 9) THEN
 D1SRADTK(KIDIAF:KFDIAF) =(PDHTLS(KIDIA:KFDIA,1,1)*(PDHTLS(KIDIA:KFDIA,1, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,2,1)*(PDHTLS(KIDIA:KFDIA,2, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,3,1)*(PDHTLS(KIDIA:KFDIA,3, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,4,1)*(PDHTLS(KIDIA:KFDIA,4, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,5,1)*(PDHTLS(KIDIA:KFDIA,5, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,6,1)*(PDHTLS(KIDIA:KFDIA,6, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,7,1)*(PDHTLS(KIDIA:KFDIA,7, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,8,1)*(PDHTLS(KIDIA:KFDIA,8, 2))**4+ &
     &     PDHTLS(KIDIA:KFDIA,10,1)*(PDHTLS(KIDIA:KFDIA,10, 2))**4)**(0.25_JPRB)
ENDIF
D1SALB(KIDIAF:KFDIAF) = PALB(KIDIA:KFDIA)
D1SVEGEV(KIDIAF:KFDIAF) = 0.		! not used
D1SDSH(KIDIAF:KFDIAF) = 0.		! DelSoilHeat of skin layer (0)
D1STNDH(KIDIAF:KFDIAF) = PDHTSS(KIDIA:KFDIA,1,15)     ! DelColdCont

D1SSDS(KIDIAF:KFDIAF)=PTSPHY*PSNSE1(KIDIA:KFDIA,1)
D1SWLDS(KIDIAF:KFDIAF)=PTSPHY*PWLE1(KIDIA:KFDIA)
!write(6,*) 'delint',D1SWLDS

! Water budgets
D1SLSRF(KIDIAF:KFDIAF) = PLSRF(KIDIA:KFDIA)
D1SCRF(KIDIAF:KFDIAF) = PCRF(KIDIA:KFDIA)
D1SLSSF(KIDIAF:KFDIAF) = PLSSF(KIDIA:KFDIA) 
D1SCSF(KIDIAF:KFDIAF) = PCSF(KIDIA:KFDIA)
D1STE(KIDIAF:KFDIAF) = PDIFTQ(KIDIA:KFDIA,KLEV)
D1STSRO(KIDIAF:KFDIAF) = PFWRO1(KIDIA:KFDIA)
D1STRO(KIDIAF:KFDIAF) = PFWRO1(KIDIA:KFDIA) + PFWROD(KIDIA:KFDIA)
D1SMLT(KIDIAF:KFDIAF) = PFWMLT(KIDIA:KFDIA)

! Screen level 
D1T2M(KIDIAF:KFDIAF) = PT2M(KIDIA:KFDIA)
D1D2M(KIDIAF:KFDIAF) = PD2M(KIDIA:KFDIA)
! forcing (end of timestep)
D1STAIR(KIDIAF:KFDIAF) = PTAIR(KIDIA:KFDIA)
D1SQAIR(KIDIAF:KFDIAF) = PQAIR(KIDIA:KFDIA)
D1SUWIND(KIDIAF:KFDIAF) = PUWIND(KIDIA:KFDIA)
D1SVWIND(KIDIAF:KFDIAF) = PVWIND(KIDIA:KFDIA)
D1SPSURF(KIDIAF:KFDIAF) = PPSURF(KIDIA:KFDIA)

! Biogenic VOC emission diagnostics per vegetation type. Check how this is computed!
D1SBVOCST1(KIDIAF:KFDIAF,:) = PDHBVOCS(KIDIA:KFDIA,:,1)
D1SBVOCST2(KIDIAF:KFDIAF,:) = PDHBVOCS(KIDIA:KFDIA,:,2)

RETURN

END SUBROUTINE UPDDIAG
